﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoccaStore.Data.Entity.Model;

namespace RoccaStore.Data.Entity.Entities
{
    public class SlugEntities : EntitiesBase<RoccaStoreEntities>, ISlugRepository
    {
        public int Save(Slug slug)
        {
            if(slug.ID == 0)
                Context.Slug.Add(slug);            

            Context.SaveChanges();
            return slug.ID;
        }

        public Slug GetByName(string name)
        {
            return Context.Slug
                .Include("Parent")
                .Include("File")
                .Include("Files")
                .FirstOrDefault(s => s.Name == name);
        }

        public void Delete(int id)
        {
            var slug = Context.Slug.FirstOrDefault(s => s.ID == id);
            slug.Deleted = DateTime.Now;
            Context.SaveChanges();
        }

        public Slug GetByID(int id)
        {
            return Context.Slug
                .Include("Parent")
                .Include("File")
                .Include("Files")
                .FirstOrDefault(s => s.ID == id);
        }

        public List<Slug> GetByParent(string name)
        {
            return Context.Slug
                .Include("Parent")
                .Include("File")
                .Include("Files")
                .Where(s => s.Parent.Name == name).ToList();
        }


        public List<Slug> GetByParent(int id)
        {
            return Context.Slug
                .Include("Parent")
                .Include("File")
                .Include("Files")
                .Where(s => s.ParentID == id).ToList();
        }
    }
}
